import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { SelectivePreloadingStrategy } from '../core/shared/selective-preloading-strategy';

@Component({
    template: `
    <p>Dashboard</p>

    <p>Session ID: {{ sessionId$ | async }}</p>
    <a id="anchor"></a>
    <p>Token: {{ token$ | async }}</p>
    `
})
export class AdminDashboardComponent implements OnInit {
    sessionId$: Observable<string>;
    token$: Observable<string>;
    modules: string[];

    constructor(
        private route: ActivatedRoute,
        private preloadingStrategy: SelectivePreloadingStrategy
    ) {
        this.modules = preloadingStrategy.preloadedModules;
    }

    ngOnInit(): void {
        // Capture the session ID if available
        this.sessionId$ = this.route
            .queryParamMap
            .pipe(map(params => params.get('session_id') || 'None'));

        // Capture the fragment if available
        this.token$ = this.route
            .fragment
            .pipe(map(fragment => fragment || 'None'));
    }
}